﻿package com.xiaoyao.novel.action.page;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import com.xiaoyao.novel.common.Pagin;
import com.xiaoyao.novel.pojo.Book;
import com.xiaoyao.novel.pojo.Category;
import com.xiaoyao.novel.pojo.Chapter;
import com.xiaoyao.novel.pojo.WebInfo;
import com.xiaoyao.novel.recovery.RecoveryMain;

public class IndexAction extends NovelAction {
	private static final long serialVersionUID = 1L;

	Random random = new Random();

	public String recovery() throws IOException{
		RecoveryMain.start() ;
		return SUCCESS ;
	}
	
	/**
	 * @deprecated PageVelocityStatic  VM 静态化.
	 */
	@Override
	public String execute() throws Exception {
		try {
			getConfig();
			List<Category> list =listCategory();
			getFooter();
			// /////////////////////
			int size = 20;
			int superSize = 9;
			List<Book> superList = getSuper(superSize);
//			getNew(size); // 最新的书籍
//			getHot(size);  // 最热的书籍
			// getRecommendIndex(size, superSize);// 暂时没用

			WebInfo webInfo = webInfoService.getStaticWebInfo();
			request.setAttribute("webInfo", webInfo);

			// 首页书籍
			String[] indexBookIds = webInfo.getIndexBookId().trim().split(",");
			Pagin pagin = new Pagin(); 
			List<Book> indexBookList = new ArrayList<Book>();
			for (int i = 0; i < indexBookIds.length; i++) {
				logger.info("首页书籍ID:" + indexBookIds[i]);
				pagin = new Pagin();
				pagin.getSearchParam().put("bookId", indexBookIds[i]);
				indexBookList.addAll(bookService.search(pagin));
			}
			request.setAttribute("indexBookList", indexBookList);
			request.setAttribute("book", indexBookList.get(0));
			int indexChapterSize=32;
			// 首页书籍章节
			for (Book book : indexBookList) {
				pagin = new Pagin();
				pagin.setPageSize(indexChapterSize);// 首页最多展示章节数量
				pagin.getSearchParam().put("orderBy", " cid asc ");
				pagin.getSearchParam().put("bookId", book.getBookId() + "");
				List<Chapter> chapterList = chapterService.search(pagin);
				int chapterSize =chapterList.size();
				for (int i = chapterSize-1; i >=0 && i>=chapterSize-8 ; i--) {
					Chapter chapter = chapterList.get(i);
					chapter.setChapterTitle("<font color='red'><B>" + chapter.getChapterTitle() + "</B></font>");
				}
//				Collections.reverse(chapterList);
				book.setList(chapterList);
			}
			
			//分类前十本书籍
			for (Category category :list) { 
				List<Book> bookList = categoryService.listCount(category.getCateId(),10);
				category.setBookList(bookList);
			}
			request.setAttribute("categoryList", list);
			return super.execute();
		} catch (Exception e) { 
			logger.error("IndexAction 错误:",e) ;
		}

		return super.execute();
	}

}
